package com.koron.order.risk.riskFollow.service.impl;


import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.business.common.entity.TOrderNoMax;
import com.koron.common.core.business.common.service.AttachmentService;
import com.koron.common.core.util.CodeTools;
import com.koron.order.risk.base.bean.convertor.LineRiskConvertor;
import com.koron.order.risk.base.bean.dto.LineRiskDTO;
import com.koron.order.risk.base.bean.po.LineRiskDetailBuilding;
import com.koron.order.risk.base.bean.po.LineRiskDetailConstruction;
import com.koron.order.risk.base.bean.po.LineRiskDetailDefault;
import com.koron.order.risk.base.bean.po.LineRiskMeasure;
import com.koron.order.risk.base.service.*;
import com.koron.order.risk.riskFollow.bean.dto.LineRiskFollowDTO;
import com.koron.order.risk.riskFollow.bean.po.LineRiskFollow;
import com.koron.order.risk.riskFollow.bean.query.LineRiskFollowQuery;
import com.koron.order.risk.riskFollow.bean.vo.LineRiskFollowVO;
import com.koron.order.risk.riskFollow.mapper.LineRiskFollowMapper;
import com.koron.order.risk.riskFollow.service.LineRiskFollowService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * 风险跟进单(LineRiskFollow)表服务实现类
 *
 * @author jzp
 * @since 2022-12-16 11:33:16
 */
@Service
public class LineRiskFollowServiceImpl implements LineRiskFollowService {

    @Autowired
    private LineRiskDetailBuildingService lineRiskDetailBuildingService;
    @Autowired
    private LineRiskDetailConstructionService lineRiskDetailConstructionService;
    @Autowired
    private LineRiskDetailDefaultService lineRiskDetailDefaultService;
    @Autowired
    private LineRiskMeasureService lineRiskMeasureService;
    @Autowired
    private LineRiskService lineRiskService;
    @Autowired
    private LineRiskConvertor lineRiskConvertor;
    @Autowired
    private AttachmentService attachmentService;

    /**
     * 通过ID查询单条数据
     *
     */
    @Override
    @TaskAnnotation("getById")
    public LineRiskFollowVO getById(SessionFactory factory, String id) {
        LineRiskFollowMapper mapper = factory.getMapper(LineRiskFollowMapper.class);
        return mapper.selectById(id);
    }

    @Override
    @TaskAnnotation("getDtoById")
    public LineRiskFollowDTO getDtoById(SessionFactory factory, String id) {
        // 查询基础信息
        LineRiskFollowVO lineRiskFollowVO = getById(factory, id);
        LineRiskFollowDTO lineRiskFollowDTO = lineRiskConvertor.followVoToFollowDto(lineRiskFollowVO);

        // 查询工单基本信息
        LineRiskDTO dto = lineRiskService.getDtoById(factory, lineRiskFollowDTO.getOrderId());
        // 清空id信息
        if (dto.getLineRiskMeasureList() != null) {
            dto.getLineRiskMeasureList().forEach(e -> e.setId(null));
        }
        if (dto.getLineRiskDetailConstruction() != null) {
            dto.getLineRiskDetailConstruction().setId(null);
        }
        if (dto.getLineRiskDetailBuilding() != null) {
            dto.getLineRiskDetailBuilding().setId(null);
        }
        if (dto.getLineRiskDetailDefault() != null) {
            dto.getLineRiskDetailDefault().setId(null);
        }
        lineRiskFollowDTO.setLineRiskDTO(dto);

        // 查询管控措施
        LineRiskMeasure lineRiskMeasure = new LineRiskMeasure();
        lineRiskMeasure.setRiskId(id);
        List<LineRiskMeasure> list = lineRiskMeasureService.list(factory, lineRiskMeasure);
        lineRiskFollowDTO.setLineRiskMeasureList(list);

        // 查询风险详情
        if ("施工作业区".equals(dto.getRiskKeyword())) {
            LineRiskDetailConstruction lineRiskDetailConstruction = lineRiskDetailConstructionService.getOneByRiskId(factory, id);
            lineRiskFollowDTO.setLineRiskDetailConstruction(lineRiskDetailConstruction);
        } else if ("违章建筑".equals(dto.getRiskKeyword())) {
            LineRiskDetailBuilding lineRiskDetailBuilding = lineRiskDetailBuildingService.getOneByRiskId(factory, id);
            lineRiskFollowDTO.setLineRiskDetailBuilding(lineRiskDetailBuilding);
        } else {
            LineRiskDetailDefault lineRiskDetailDefault = lineRiskDetailDefaultService.getOneByRiskId(factory, id);
            lineRiskFollowDTO.setLineRiskDetailDefault(lineRiskDetailDefault);
        }
        return lineRiskFollowDTO;
    }

    /**
     * 查询所有数据
     *
     */
    @Override
    @TaskAnnotation("list")
    public List<LineRiskFollowVO> list(SessionFactory factory, LineRiskFollowQuery query) {
        LineRiskFollowMapper mapper = factory.getMapper(LineRiskFollowMapper.class);
        return mapper.selectList(query);
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<LineRiskFollowVO> page(SessionFactory factory, PageQuery pageQuery, LineRiskFollowQuery query) {
        pageQuery.unPrefixOrders("lineRiskFollow", Arrays.asList("currNodeName",
                "currNodeUser",
                "currNodeCode"));
        return LineRiskFollowService.super.page(factory, pageQuery, query);
    }

    /**
     * 新增数据
     *
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, LineRiskFollowVO lineRiskFollowVO) {
        if (lineRiskFollowVO.getStatus() == null) {
            lineRiskFollowVO.setStatus("0");
        }
        LineRiskFollowMapper mapper = factory.getMapper(LineRiskFollowMapper.class);
        return mapper.insertOrUpdate(lineRiskFollowVO) > 0;
    }

    @Override
    @TaskAnnotation("saveDto")
    public LineRiskFollowDTO saveDto(SessionFactory factory, LineRiskFollowDTO lineRiskFollowDTO) {
        // 保存主表数据
        if (lineRiskFollowDTO.getCode() == null) {
            String prefix = lineRiskFollowDTO.getOrderCode() + "_GJ";
            TOrderNoMax tOrderNoMax = CodeTools.getNoMax(prefix, "y");
            String code = prefix + String.format("%03d", tOrderNoMax.getMaxValue());
            lineRiskFollowDTO.setCode(code);
        }
        save(factory, lineRiskFollowDTO);
        String riskFollowId = lineRiskFollowDTO.getId();

        // 保存管控措施
        lineRiskMeasureService.removeByRiskId(factory, riskFollowId);
        if (lineRiskFollowDTO.getLineRiskMeasureList() != null) {
            if (lineRiskFollowDTO.getLineRiskMeasureList().size() != 0 && !riskFollowId.equals(lineRiskFollowDTO.getLineRiskMeasureList().get(0).getRiskId())) {
                lineRiskFollowDTO.getLineRiskMeasureList().forEach(e -> e.setId(null));
            }
            lineRiskFollowDTO.getLineRiskMeasureList().forEach(e -> e.setRiskId(riskFollowId));
            lineRiskMeasureService.saveBatch(factory, lineRiskFollowDTO.getLineRiskMeasureList());
        }

        // 保存风险详情
        if (lineRiskFollowDTO.getLineRiskDetailConstruction() != null) {
            if (!riskFollowId.equals(lineRiskFollowDTO.getLineRiskDetailConstruction().getRiskId())) {
                lineRiskFollowDTO.getLineRiskDetailConstruction().setId(null);
            }
            lineRiskFollowDTO.getLineRiskDetailConstruction().setRiskId(riskFollowId);
            lineRiskDetailConstructionService.save(factory, lineRiskFollowDTO.getLineRiskDetailConstruction());
        }
        if (lineRiskFollowDTO.getLineRiskDetailBuilding() != null) {
            if (!riskFollowId.equals(lineRiskFollowDTO.getLineRiskDetailBuilding().getRiskId())) {
                lineRiskFollowDTO.getLineRiskDetailBuilding().setId(null);
            }
            lineRiskFollowDTO.getLineRiskDetailBuilding().setRiskId(riskFollowId);
            lineRiskDetailBuildingService.save(factory, lineRiskFollowDTO.getLineRiskDetailBuilding());
        }
        if (lineRiskFollowDTO.getLineRiskDetailDefault() != null) {
            if (!riskFollowId.equals(lineRiskFollowDTO.getLineRiskDetailDefault().getRiskId())) {
                lineRiskFollowDTO.getLineRiskDetailDefault().setId(null);
            }
            lineRiskFollowDTO.getLineRiskDetailDefault().setRiskId(riskFollowId);
            lineRiskDetailDefaultService.save(factory, lineRiskFollowDTO.getLineRiskDetailDefault());
        }

        return lineRiskFollowDTO;
    }

    /**
     * 修改数据
     *
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, LineRiskFollow lineRiskFollow) {
        LineRiskFollowMapper mapper = factory.getMapper(LineRiskFollowMapper.class);
        return mapper.update(lineRiskFollow) > 0;
    }

    @Override
    @TaskAnnotation("updateStatus")
    public boolean updateStatus(SessionFactory factory, String id, String status) {
        LineRiskFollow lineRiskFollow = new LineRiskFollow();
        lineRiskFollow.setId(id);
        lineRiskFollow.setStatus(status);
        return update(factory, lineRiskFollow);
    }

    @Override
    @TaskAnnotation("end")
    public boolean end(SessionFactory factory, LineRiskFollowDTO lineRiskFollowDTO) {
        // 保存基本信息
        lineRiskFollowDTO.setStatus("2");
        saveDto(factory, lineRiskFollowDTO);

        // 修改风险管控单的数据 措施落实情况、风险详细信息
        LineRiskDTO riskDTO = lineRiskService.getDtoById(factory, lineRiskFollowDTO.getOrderId());
        riskDTO.setLineRiskMeasureList(lineRiskFollowDTO.getLineRiskMeasureList());
        riskDTO.getLineRiskMeasureList().forEach(e -> e.setId(null));

        String lineRiskDetailConstructionId = riskDTO.getLineRiskDetailConstruction() == null ? null : riskDTO.getLineRiskDetailConstruction().getId();
        riskDTO.setLineRiskDetailConstruction(lineRiskFollowDTO.getLineRiskDetailConstruction());
        Optional.ofNullable(riskDTO.getLineRiskDetailConstruction()).ifPresent(e -> e.setId(lineRiskDetailConstructionId));

        String lineRiskDetailBuildingId = riskDTO.getLineRiskDetailBuilding() == null ? null : riskDTO.getLineRiskDetailBuilding().getId();
        riskDTO.setLineRiskDetailBuilding(lineRiskFollowDTO.getLineRiskDetailBuilding());
        Optional.ofNullable(riskDTO.getLineRiskDetailBuilding()).ifPresent(e -> e.setId(lineRiskDetailBuildingId));

        String lineRiskDetailDefaultId = riskDTO.getLineRiskDetailDefault() == null ? null : riskDTO.getLineRiskDetailDefault().getId();
        riskDTO.setLineRiskDetailDefault(lineRiskFollowDTO.getLineRiskDetailDefault());
        Optional.ofNullable(riskDTO.getLineRiskDetailDefault()).ifPresent(e -> e.setId(lineRiskDetailDefaultId));

        lineRiskService.saveDto(factory, riskDTO);

        // 复制附件
        LineRiskFollowDTO lineRiskFollowDTOSource = getDtoById(factory, lineRiskFollowDTO.getId());
        if (lineRiskFollowDTOSource.getLineRiskMeasureList() != null) {
            for (int i = 0; i < lineRiskFollowDTOSource.getLineRiskMeasureList().size(); i++) {
                LineRiskMeasure lineRiskMeasure = lineRiskFollowDTOSource.getLineRiskMeasureList().get(i);
                LineRiskMeasure lineRiskMeasure1 = riskDTO.getLineRiskMeasureList().get(i);
                attachmentService.copyAttachment(lineRiskMeasure1.getId(), lineRiskMeasure.getId());
            }
        }
        boolean a = lineRiskFollowDTOSource.getLineRiskDetailConstruction() != null &&
                attachmentService.copyAttachment(riskDTO.getLineRiskDetailConstruction().getId(),
                        lineRiskFollowDTOSource.getLineRiskDetailConstruction().getId()) > 0;

        boolean b = lineRiskFollowDTOSource.getLineRiskDetailBuilding() != null &&
                attachmentService.copyAttachment(riskDTO.getLineRiskDetailBuilding().getId(),
                        lineRiskFollowDTOSource.getLineRiskDetailBuilding().getId()) > 0;

        boolean c = lineRiskFollowDTOSource.getLineRiskDetailDefault() != null &&
                attachmentService.copyAttachment(riskDTO.getLineRiskDetailDefault().getId(),
                        lineRiskFollowDTOSource.getLineRiskDetailDefault().getId()) > 0;

        return true;
    }

    /**
     * 通过主键删除数据
     *
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        LineRiskFollowMapper mapper = factory.getMapper(LineRiskFollowMapper.class);
        return mapper.hidden(id) > 0;
    }
}
